package entity

import (
	"encoding/json"
	"time"
)

type BossPoints struct {
	RoleID    int32 `gorm:"primary_key;AUTO_INCREMENT"`
	ServerID  int32
	Points    int32
	Kill      int32 // 击杀时间戳
	Date      int32
	IDsData   []byte `gorm:"type:jsonb"`
	CreatedAt time.Time
	UpdatedAt time.Time

	IDsMap map[int32]int32 `sql:"-"`
}

func (i *BossPoints) AfterFind() error {

	if len(i.IDsData) > 0 {

		i.IDsMap = make(map[int32]int32)
		return json.Unmarshal(i.IDsData, &i.IDsMap)
	}

	return nil
}

func (i *BossPoints) BeforeSave() (err error) {

	if i.IDsMap == nil {
		i.IDsMap = make(map[int32]int32)
	}

	i.IDsData, err = json.Marshal(i.IDsMap)
	return err
}

func (i *BossPoints) BeforeUpdate() (err error) {

	if i.IDsMap == nil {
		i.IDsMap = make(map[int32]int32)
	}

	i.IDsData, err = json.Marshal(i.IDsMap)
	return err
}
